<?php

/* Portal Pracy - praca magisterska
 * autor: Tomek Ceszke
 * email: tomek.ceszke@gmail.com
 * Politechnika Koszalińska 2007
 */

class AdsController extends AppController
{
	var $name 					= 'Ads';
	var $othAuthRestrictions 	= array('add','edit','delete','employer','view_emp');
	var $uses 					= array('Ad','Company','Region','Category','Tag');
	var $helpers 				= array('Time');

	function index()
	{
		$defCriteria = aa(
				"Ad.public_date", "<= ".date('Y-m-d'),
				"Ad.validity_date", "> ".date('Y-m-d'),
				"Ad.status",'1'
				);

				$criteria = aa(
					"Ad.public_date", "<= ".date('Y-m-d'),
					"Ad.validity_date", "> ".date('Y-m-d'),
					"Ad.status",'1'
					);

					// kryteria
					if(isset($this->params['url']['data']) AND is_array($this->params['url']['data']))
					{
						$fields = $this->params['url']['data'];

						//kategoria
						if(isset($fields['Ad']['category_id']) AND $fields['Ad']['category_id']!=0)
						{
							$criteria['Ad.category_id'] = $fields['Ad']['category_id'];

						}
							
						//region
						if(isset($fields['Ad']['region_id']) AND $fields['Ad']['region_id']!=1)
						{
							$criteria['Ad.region_id'] = $fields['Ad']['region_id'];

						}
							
						//firma
						if(isset($fields['Company']['name']) AND $fields['Company']['name']!="")
						{
							$criteria['Company.name'] = "LIKE ".$fields['Company']['name']."%";
						}
							
							
						//tagi
						$a = array();
						if(isset($fields['Tag']['name']) AND is_array($fields['Tag']['name']))
						{
							$a = array_diff($fields['Tag']['name'],array(""));
						}

						if(!empty($a))
						{
							$tagCriteria['Tag.name'] = $fields['Tag']['name'];

							//$tagData array id tagow
							$tagData = $this->Tag->generateList($tagCriteria);

							if(is_array($tagData))
							{
								$adIds = array();
		
								// zwraca $adIds[k]=>array(v1,v2,..) gdzie k - liczba tagow, v1,.. - oferty pasujace do nich
								foreach (array_keys($tagData) as $k => $tagid)
								{
									//dla kazdego taga pobierz id oferty
									$tmpData = $this->Tag->query("SELECT `id` FROM `ads` AS `Ad` JOIN `ads_tags` ON `ads_tags`.`tag_id` = '".$tagid."' AND `ads_tags`.`ad_id` = `Ad`.`id` WHERE `Ad`.`status` = 1");

									//przepisz na kryteria TODO: Za duzo danych!!!
									if(is_array($tmpData))
									{
										foreach ($tmpData as $tmpItem)
										{
											$adIds[$k][] = $tmpItem['Ad']['id'];
										}
									}else
									{
										$adIds[$k][] = '0';
									}
								}
								//echo '<pre>';
								//var_dump($adIds);
								//echo '</pre>';
									
								//ile tagow
								switch (count($adIds))
								{
									case 1:
										$preCriteria = $adIds[0];
										break;
									case 2:
										$preCriteria = array_values(array_intersect($adIds[0],$adIds[1]));
										break;
									case 3:
										$preCriteria = array_values(array_intersect($adIds[0],$adIds[1],$adIds[2]));
								}
									
								if(!empty($preCriteria))
								{
									$criteria['Ad.id'] = $preCriteria;

								}else
								{
									$criteria['Ad.id'] = 0;
								}

							}else
							{
								$criteria['Ad.id'] = 0;
							}
						} // tagi
					}//kryteria


					$fields = array(
		"Ad.id",
		"Ad.position",
		"Ad.public_date",
		"Ad.city",
		"Company.id",
		"Company.name",
		"Company.status",
		"Region.id",
		"Region.name",
		"Category.id",
		"Category.name",

		);

		list($order,$limit,$page) = $this->Pagination->init($criteria); // Added
		$this->Pagination->sortBy = 'Ad.public_date';
		$this->Pagination->direction = 'DESC';

		$this->set('filter',(array_diff($criteria,$defCriteria)));

		$data = $this->Ad->findAll($criteria,$fields,'Ad.public_date DESC', $limit, $page,1); // Extra parameters added

		$this->set('data',$data);

		if($this->RequestHandler->isAjax())
		{
			$this->set('xhttp',true);
		}
	}

	function rss()
	{
		$this->layout = 'xml';

		$defCriteria = aa(
				"Ad.public_date", "<= ".date('Y-m-d'),
				"Ad.validity_date", "> ".date('Y-m-d'),
				"Ad.status",'1'
				);

				$criteria = aa(
					"Ad.public_date", "<= ".date('Y-m-d'),
					"Ad.validity_date", "> ".date('Y-m-d'),
					"Ad.status",'1'
					);

					// kryteria
					if(isset($this->params['url']['data']) AND is_array($this->params['url']['data']))
					{
						$fields = $this->params['url']['data'];

						//kategoria
						if(isset($fields['Ad']['category_id']) AND $fields['Ad']['category_id']!=0)
						{
							$criteria['Ad.category_id'] = $fields['Ad']['category_id'];

						}
							
						//region
						if(isset($fields['Ad']['region_id']) AND $fields['Ad']['region_id']!=1)
						{
							$criteria['Ad.region_id'] = $fields['Ad']['region_id'];

						}
							
						//firma
						if(isset($fields['Company']['name']) AND $fields['Company']['name']!="")
						{
							$criteria['Company.name'] = "LIKE ".$fields['Company']['name']."%";
						}
							
							
						//tagi
						$a = array();
						if(isset($fields['Tag']['name']) AND is_array($fields['Tag']['name']))
						{
							$a = array_diff($fields['Tag']['name'],array(""));
						}

						if(!empty($a))
						{
							$tagCriteria['Tag.name'] = $fields['Tag']['name'];

							//$tagData array id tagow
							$tagData = $this->Tag->generateList($tagCriteria);

							if(is_array($tagData))
							{
								$adIds = array();
		
								// zwraca $adIds[k]=>array(v1,v2,..) gdzie k - liczba tagow, v1,.. - oferty pasujace do nich
								foreach (array_keys($tagData) as $k => $tagid)
								{
									//dla kazdego taga pobierz id oferty
									$tmpData = $this->Tag->query("SELECT `id` FROM `ads` AS `Ad` JOIN `ads_tags` ON `ads_tags`.`tag_id` = '".$tagid."' AND `ads_tags`.`ad_id` = `Ad`.`id` WHERE `Ad`.`status` = 1");

									//przepisz na kryteria TODO: Za duzo danych!!!
									if(is_array($tmpData))
									{
										foreach ($tmpData as $tmpItem)
										{
											$adIds[$k][] = $tmpItem['Ad']['id'];
										}
									}else
									{
										$adIds[$k][] = '0';
									}
								}
								//echo '<pre>';
								//var_dump($adIds);
								//echo '</pre>';
									
								//ile tagow
								switch (count($adIds))
								{
									case 1:
										$preCriteria = $adIds[0];
										break;
									case 2:
										$preCriteria = array_values(array_intersect($adIds[0],$adIds[1]));
										break;
									case 3:
										$preCriteria = array_values(array_intersect($adIds[0],$adIds[1],$adIds[2]));
								}
									
								if(!empty($preCriteria))
								{
									$criteria['Ad.id'] = $preCriteria;

								}else
								{
									$criteria['Ad.id'] = 0;
								}

							}else
							{
								$criteria['Ad.id'] = 0;
							}
						} // tagi
					}//kryteria


					$fields = array(
		"Ad.id",
		"Ad.position",
		"Ad.public_date",
		"Ad.city",
		"Company.id",
		"Company.name",
		"Company.status",
		"Region.id",
		"Region.name",
		"Category.id",
		"Category.name",

		);

		$data = $this->Ad->findAll($criteria,$fields,'Ad.public_date DESC', 15, null,1); // Extra parameters added

		$this->set('data',$data);
		$this->set('filter',(array_diff($criteria,$defCriteria)));

	//print_r($data);
	}




	function employer()
	{

		$defCriteria = aa(
		"Company.user_id", $this->othAuth->user('id')
		);

		$criteria = aa(
		"Company.user_id", $this->othAuth->user('id')
		);

		// kryteria
		if(isset($this->params['url']['data']) AND is_array($this->params['url']['data']))
		{
			$fields = $this->params['url']['data'];

			//ukryte
			if(isset($fields['Ad']['hidden']) AND $fields['Ad']['hidden']!=0)
			{
				$criteria['Ad.status'] = '0';
			}

			//aktualne
			if(isset($fields['Ad']['current']) AND $fields['Ad']['current']!=0)
			{
				$criteria["Ad.public_date"] =  "<= ".date('Y-m-d');
				$criteria["Ad.validity_date"] = "> ".date('Y-m-d');
				$criteria['Ad.status'] = '1';
			}

			//kategoria
			if(isset($fields['Ad']['category_id']) AND $fields['Ad']['category_id']!=0)
			{
				$criteria['Ad.category_id'] = $fields['Ad']['category_id'];

			}

			//region
			if(isset($fields['Ad']['region_id']) AND $fields['Ad']['region_id']!=1)
			{
				$criteria['Ad.region_id'] = $fields['Ad']['region_id'];

			}

			//firma
			if(isset($fields['Company']['name']) AND $fields['Company']['name']!="")
			{
				$criteria['Company.name'] = "LIKE ".$fields['Company']['name']."%";
			}

			$a = array();

			if(isset($fields['Tag']['name']) AND is_array($fields['Tag']['name']))
			{
				$a = array_diff($fields['Tag']['name'],array(""));
			}


			//print_r($a);
			if(!empty($a))
			{
				$tagCriteria['Tag.name'] = $fields['Tag']['name'];

				//$tagData array id tagow
				$tagData = $this->Tag->generateList($tagCriteria);

				if(is_array($tagData))
				{
					// zwraca $adIds[k]=>array(v1,v2,..) gdzie k - liczba tagow, v1,.. - oferty pasujace do nich
					foreach (array_keys($tagData) as $k => $tagid)
					{
						//dla kazdego taga pobierz id oferty
						$tmpData = $this->Tag->query("SELECT `id` FROM `ads` AS `Ad` JOIN `ads_tags` ON `ads_tags`.`tag_id` = '".$tagid."' AND `ads_tags`.`ad_id` = `Ad`.`id` WHERE `Ad`.`status` = 1");

						//przepisz na kryteria TODO: Za duzo danych!!!
						if(is_array($tmpData))
						{
							foreach ($tmpData as $tmpItem)
							{
								$adIds[$k][] = $tmpItem['Ad']['id'];
							}
						}
					}

					//ile tagow
					switch (count($adIds))
					{
						case 1:
							$preCriteria = $adIds[0];
							break;
						case 2:
							$preCriteria = array_values(array_intersect($adIds[0],$adIds[1]));
							break;
						case 3:
							$preCriteria = array_values(array_intersect($adIds[0],$adIds[1],$adIds[2]));
					}

					if(!empty($preCriteria))
					{
						$criteria['Ad.id'] = $preCriteria;

					}else
					{
						$criteria['Ad.id'] = 0;
					}

				}else
				{
					$criteria['Ad.id'] = 0;
				}
			} // tagi
		}//kryteria

		//print_r($criteria);

		$fields = array(
		"Ad.id",
		"Ad.position",
		"Ad.public_date",
		"Ad.city",
		"Ad.status",
		"Company.id",
		"Company.name",
		"Company.status",
		"Category.id",
		"Category.name",
		"Region.id",
		"Region.name",
		"Ad.validity_date",
		);

		list($order,$limit,$page) = $this->Pagination->init($criteria); // Added
		$data = $this->Ad->findAll($criteria,$fields, 'Ad.public_date DESC', $limit, $page,1); // Extra parameters added

		$this->set('filter',(array_diff($criteria,$defCriteria)));
		$this->set('data',$data);


		if($this->RequestHandler->isAjax())
		{
			$this->set('xhttp',true);
		}
	}



	function view($id)
	{
		$criteria = aa(
			"Ad.id",$id,
			"Ad.public_date", "<= ".date('Y-m-d'),
			"Ad.validity_date", "> ".date('Y-m-d'),
			"Ad.status",'1');
		
			$fields = array(

		"Company.id",
		"Company.logo",		
		"Company.name",
		"Company.info",
		"Company.career_emails",
		"Company.career_phones",
		"Company.career_persons",
		"Company.career_www",
		"Company.status",

		"Ad.position",
		"Ad.city",
		"Ad.ref_no",
		"Ad.brief",
		"Ad.requirements",
		"Ad.optional",
		"Ad.benefits",
		"Ad.career_txt",

		"Region.id",
		"Region.name",

		"Category.id",
		"Category.name",
		);

		$data = $this->Ad->find($criteria,$fields,null,null,0);

		if(!is_array($data))
		{
			$this->flash('Nie znaleziono wybranej oferty!','/ads/index');
			exit();
		}

		$this->set('data', $data);
	}


	function view_emp($id)
	{
		$criteria =
		aa
		(
			"Ad.id",$id,
			"Company.user_id",$this->othAuth->user('id')						
			);

			$fields = array(

		"Company.id",
		"Company.logo",
		"Company.name",
		"Company.info",
		"Company.career_emails",
		"Company.career_phones",
		"Company.career_persons",
		"Company.career_www",

		"Ad.id",
		"Ad.position",
		"Ad.city",
		"Ad.ref_no",
		"Ad.brief",
		"Ad.requirements",
		"Ad.optional",
		"Ad.benefits",
		"Ad.career_txt",
		"Ad.status",
		"Ad.validity_date",
		"Ad.public_date",

		"Region.id",
		"Region.name",

		"Category.id",
		"Category.name",
		);

		$data = $this->Ad->find($criteria,$fields,null,null,0);

		if(!is_array($data))
		{
			$this->flash('Nie znaleziono wybranej oferty!','/ads/employer');
			exit();
		}

		$this->set('data', $data);
	}


	function delete($id)
	{
		$this->autoRender=false;
		$criteria ='Company.user_id='.$this->othAuth->user('id').' AND Ad.id='.$id;

		if($this->Ad->findCount($criteria)==1)
		{
			$this->Ad->del($id);

			if(!$this->RequestHandler->isAjax())
			{
				$this->redirect('/ads/employer');
			}

			$this->employer();
			$this->render('employer');
		}
		else
		{
			if(!$this->RequestHandler->isAjax())
			{
				$this->flash('Naruszenie zasad bezpieczeństwa !','/');
				exit();
			}
		}
	}

	function uhide($id,$view = null)
	{
		$this->autoRender=false;
		$criteria ="Company.user_id=".$this->othAuth->user('id')." AND Ad.id=".$id." AND Ad.public_date <= '".date('Y-m-d')."' AND Ad.validity_date > '".date('Y-m-d')."'";

		if($this->Ad->findCount($criteria)==1)
		{
			$this->Ad->id=$id;
			$current = $this->Ad->read('status');

			if($current['Ad']['status']==0)
			{
				$this->Ad->saveField('status',1);
			}

			if($current['Ad']['status']==1)
			{
				$this->Ad->saveField('status',0);
			}

			if(!$this->RequestHandler->isAjax())
			{
				if($view==null)
				{
					$this->redirect('/ads/employer');
				}else
				{
					$this->redirect('/ads/view_emp/'.$id);
				}
			}


			$this->employer();
			$this->render('employer');

		}
		else
		{
			if(!$this->RequestHandler->isAjax())
			{
				$this->flash('Naruszenie zasad bezpieczeństwa !','/');
				exit();
			}
		}
	}



	function add()
	{
		$this->uses = array('Company','Region','Category','Tag');

		$user_id = $this->othAuth->user('id');

		$success=false;



		//prepare select tags data
		$companies_tmp = $this->Company->findAll('user_id = '.$user_id,a('id','name','status'));
		$regions_tmp = $this->Region->findAll(null,a('id','name'));
		$categories_tmp = $this->Category->findAll(null,a('id','name'));

		if(empty($companies_tmp))
		{
			$this->flash('
				Aby dodać ofertę pracy, najpierw musisz dodać swoją firmę do systemu.
				','/companies/add/');
			exit();
		}

		foreach ($regions_tmp as $region)
		{
			$regions[$region['Region']['id']] = $region['Region']['name'];
		}

		foreach ($categories_tmp as $category)
		{
			$categories[$category['Category']['id']] = $category['Category']['name'];
		}

		foreach ($companies_tmp as $company)
		{
			if($company['Company']['status']!=1)
			{
				$company['Company']['name'].=' (nieaktywna)';
			}
			$companies[$company['Company']['id']] = $company['Company']['name'];
		}

		$this->set('regions',$regions);
		$this->set('categories',$categories);
		$this->set('companies',$companies);


		if(!empty($this->data))
		{
			$this->data['Ad']['user_id'] = $user_id;
			//TODO: walidacja validity_date + dodac sprawdzanie wsteczne
			$this->data['Ad']['validity_date'] = date("Y-m-d",strtotime('+'.$this->data['Ad']['valid_date'].' day',strtotime($this->data['Ad']['public_date'])));

			if(!array_key_exists($this->data['Ad']['company_id'],$companies))
			{
				$this->flash('Naruszenie zasad bezpieczeństwa !','/profil/');
				exit();
			}

			$tags = array();

			if(!empty($this->data['Ad']['tag1']))
			{
				$tags[] = $this->data['Ad']['tag1'];
			}
			if(!empty($this->data['Ad']['tag2']))
			{
				$tags[] = $this->data['Ad']['tag2'];
			}
			if(!empty($this->data['Ad']['tag3']))
			{
				$tags[] = $this->data['Ad']['tag3'];
			}

			if($this->Ad->validates($this->data))
			{
				//	tagi
				if(!empty($tags))
				{
					foreach ($tags as $tag)
					{
						if($this->Tag->findCount("name = '{$tag}'")==0)
						{															
							$this->Tag->execute("INSERT INTO `tags` (`name`) VALUES ('{$tag}')");
						}
						
						$r = $this->Tag->find("name = '{$tag}'","id",null,0);
						$tag_id[] = $r['Tag']['id'];											
					}
					
					$this->data['Tag']['Tag'] = $tag_id;
					
				}

				if($this->Ad->save($this->data))
				{
					$this->flash('Dane zostały zapisane.','/ads/view_emp/'.$this->Ad->getID());
					exit();	
				}
			}
			else
			{
				$this->set('errorsFound',true);
			}
		}
	}


	function edit($id = null)
	{
		//print_r($this->Ad->read());
		if($id==null)
		{
			$this->redirect('/ads/add');
			exit();
		}

		$this->Ad->id = $id;

		$user_id = $this->othAuth->user('id');

		$success=false;

		$this->Company->recursive=0;
		$this->Region->recursive=0;
		$this->Category->recursive=0;
		$this->Tags->recursive=0;

		//prepare select tags data
		$companies_tmp = $this->Company->findAll('user_id = '.$user_id,a('id','name','status'));
		$regions_tmp = $this->Region->findAll(null,a('id','name'));
		$categories_tmp = $this->Category->findAll(null,a('id','name'));

		$sdate = $this->Ad->read(a('public_date','validity_date'));
		//print_r($sdate);

		if(empty($companies_tmp))
		{
			$this->flash('
				Aby dodać ofertę pracy, najpierw musisz dodać swoją firmę do systemu.
				','/companies/add/');
			exit();
		}

		if(  date('Y-m-d') < $sdate['Ad']['validity_date'] && date('Y-m-d') >= $sdate['Ad']['public_date'])
		{
			$this->set('active',true);
			$active = true;
		}
		else
		{
			$active = false;
		}

		foreach ($regions_tmp as $region)
		{
			$regions[$region['Region']['id']] = $region['Region']['name'];
		}

		foreach ($categories_tmp as $category)
		{
			$categories[$category['Category']['id']] = $category['Category']['name'];
		}

		foreach ($companies_tmp as $company)
		{
			if($company['Company']['status']!=1)
			{
				$company['Company']['name'].=' (nieaktywna)';
			}
			$companies[$company['Company']['id']] = $company['Company']['name'];
		}

		$this->set('regions',$regions);
		$this->set('categories',$categories);
		$this->set('companies',$companies);


		if(!empty($this->data))
		{
			$this->data['Ad']['user_id'] = $user_id;
			//TODO: walidacja validity_date + dodac sprawdzanie wsteczne

			if($active)
			{
				$this->data['Ad']['public_date']= $sdate['Ad']['public_date'];
				$this->data['Ad']['validity_date']= $sdate['Ad']['validity_date'];
			}
			else
			{
				$this->data['Ad']['validity_date'] = date("Y-m-d",strtotime('+'.$this->data['Ad']['valid_date'].' day',strtotime($this->data['Ad']['public_date'])));
				if($this->data["Ad"]["public_date"] > date('Y-m-d'))
				{
					//jesli nowa ustawiona data jest przyszla to zmien status na active
					$this->data['Ad']['status']= '1';
				}
			}

			if(!array_key_exists($this->data['Ad']['company_id'],$companies))
			{
				$this->flash('Naruszenie zasad bezpieczeństwa !','/profil/');
				exit();
			}

			$tags = array();

			if(!empty($this->data['Ad']['tag1']))
			{
				$tags[] = $this->data['Ad']['tag1'];
			}
			if(!empty($this->data['Ad']['tag2']))
			{
				$tags[] = $this->data['Ad']['tag2'];
			}
			if(!empty($this->data['Ad']['tag3']))
			{
				$tags[] = $this->data['Ad']['tag3'];
			}

			if($this->Ad->validates($this->data))
			{
				//	tagi
				if(!empty($tags))
				{
					//print_r($tag_id);
					foreach ($tags as $tag)
					{
						if($this->Tag->findCount("name = '{$tag}'")==0)
						{															
							$this->Tag->execute("INSERT INTO `tags` (`name`) VALUES ('{$tag}')");
						}
						
						$r = $this->Tag->find("name = '{$tag}'","id",null,0);
						$tag_id[] = $r['Tag']['id'];											
					}
							
					//print_r($tag_id);
					$this->data['Tag']['Tag'] = $tag_id;
				}

				if($this->Ad->save($this->data))
				{
					$this->flash('Dane zostały zapisane.','/ads/view_emp/'.$this->Ad->getID());
					exit();	
				}
			}
			else
			{
				$this->set('errorsFound',true);
			}
		}
		else
		{
			$this->data=$this->Ad->read();

			if($this->data['Company']['user_id']!=$user_id)
			{
				$this->flash('Naruszenie zasad bezpieczeństwa !','/profil');
				exit();
			}

			if(!empty($this->data['Tag'][0]))
			{
				$this->data['Ad']['tag1'] = $this->data['Tag'][0]['name'];
			}
			if(!empty($this->data['Tag'][1]))
			{
				$this->data['Ad']['tag2'] = $this->data['Tag'][1]['name'];
			}
			if(!empty($this->data['Tag'][2]))
			{
				$this->data['Ad']['tag3'] = $this->data['Tag'][2]['name'];
			}

			//echo date('Y-m-d');
			//echo $this->data['Ad']['validity_date'];

			$this->data['Ad']['valid_date'] = (AdsController::_date_diff($this->data['Ad']['public_date'],$this->data['Ad']['validity_date']));
			//echo $this->data['Ad']['valid_date'];
		}
	}



	function _getNames($field)
	{
		$mo = Inflector::classify(Inflector::pluralize($field));

		return $this->$mo->findAll(null,array('id','name'),null,null,null,0);
	}


	function getComboData($field)
	{
		$data_tmp = $this -> _getNames($field);
		$mo = Inflector::classify(Inflector::pluralize($field));

		if(!is_array($data_tmp)) return;

		foreach ($data_tmp as $item)
		{
			$data[$item[$mo]['id']] = $item[$mo]['name'];
		}

		return $data;
	}


	function getAdsNames()
	{
		return $this->Ad->findAll(null,array('id','position'),'id',5);
	}

	function getLast()
	{
		$criteria = aa(
		"Ad.public_date", "<= ".date('Y-m-d'),
		"Ad.validity_date", "> ".date('Y-m-d'),
		"Ad.status",'1'
				);
		return $this->Ad->findAll($criteria,array('Ad.id','Ad.position'),'Ad.public_date DESC',5,null,0);
	}

	function getCount()
	{
		$criteria = aa
		(
			"Ad.public_date", "<= ".date('Y-m-d'),
			"Ad.validity_date", "> ".date('Y-m-d'),
			"Ad.status",'1',
			"Company.user_id",$this->othAuth->user('id')
		);
		
		return $this->Ad->findCount($criteria);
	}
	
	function getCountAll()
	{
		$criteria = aa
		(
			"Company.user_id",$this->othAuth->user('id')
		);

		return $this->Ad->findCount($criteria);
	}


	function tagsautocomplete()
	{
		$this->autoRender=false;
		if(isset($this->data['Ad']['tag1']))
		{
			$resa = $this->Tag->findAll("name LIKE '{$this->data['Ad']['tag1']}%'");
		}
		if(isset($this->data['Ad']['tag2']))
		{
			$resa = $this->Tag->findAll("name LIKE '{$this->data['Ad']['tag2']}%'");
		}
		if(isset($this->data['Ad']['tag3']))
		{
			$resa = $this->Tag->findAll("name LIKE '{$this->data['Ad']['tag3']}%'");
		}

		if(!empty($resa))
		{
			$this->set('tags',$resa);
			$this->layout = "ajax";
			$this->render();
		}else
		{
			return false;
		}
	}

	function tagsautocomplete1()
	{
		$this->autoRender=false;
		foreach ($this->data['Tag']['name'] as $tag) {
			$resa = $this->Tag->findAll("name LIKE '{$tag}%'","name",null,null,null,0);
		}
		//var_dump($this->data);

		if(!empty($resa))
		{
			$this->set('tags',$resa);
			$this->layout = "ajax";
			$this->render();
		}else
		{
			return false;
		}
	}

	function coautocomplete1()
	{
		$this->autoRender=false;


		$resa = $this->Company->findAll("name LIKE '{$this->data['Company']['name']}%'","name",null,null,null,0);


		if(!empty($resa))
		{
			$this->set('tags',$resa);
			$this->layout = "ajax";
			$this->render();
		}else
		{
			return false;
		}
	}



	function _date_diff($date1, $date2)
	{
		//$date1  today, or any other day
		//$date2  date to check against

		$d1 = explode("-", $date1);
		$y1 = $d1[0];
		$m1 = $d1[1];
		$d1 = $d1[2];

		$d2 = explode("-", $date2);
		$y2 = $d2[0];
		$m2 = $d2[1];
		$d2 = $d2[2];

		$date1_set = mktime(0,0,0, $m1, $d1, $y1);
		$date2_set = mktime(0,0,0, $m2, $d2, $y2);

		return(round(($date2_set-$date1_set)/(60*60*24)));
	}



}
?>
